# -*- coding: utf-8 -*-

from datetime import datetime, timedelta, date, time
# pip install python-dateutil
from dateutil.relativedelta import relativedelta

import py_global
import logging

logger = logging.getLogger(__name__)

"""
按格式将字符串和日期时间相互转化:
代码     说明
%Y      4位数的年
%y      2位数的年
%m      2位数的月[01, 12]
%d      2位数的日[01, 31]
%H      时(24小时制)[00, 23]
%I      时(12小时制)[01, 12]
%M      2位数的分[00, 59]
%S      秒[00, 61](秒60和61用于闰秒)
%f

%j      每年的第几天[001, 366]
%w      每周的第几天[0, 6],星期天为每周的第一天
%U      每年的第几周[00, 53],星期天为每周的第一天,每年的第一个星期天之前的那几天被认为是第0周
%W      每年的第几周[00, 53],星期一为每周的第一天,每天的第一个星期一之前的那几天被认为是第0周

%F      %Y-%m-%d的简写形式(2018-08-08)
%D      %m/%d/%y的简写形式(08/08/2018)



%a      本地简化星期名称
%A      本地完整星期名称
%b      本地简化月份名称
%B      本地完整月份名称

%x      本地相应日期
%X      本地相应时间
%z      以+HHMMh或-HHMM形式表示的时区偏移量,如果时区为naive则返回空字符串
%Z      时区的名字[UTC,CST],如果不存在为空字符

%c      本地相应的日期和时间表示
%p      本地上午或下午的相应表示,[AM,PM]




"""
# yyyy-MM-dd HH:mm:ss.SSS
dtf = '%Y-%m-%d %H:%M:%S.%f'
# YYYY-MM-DD HH:MM:SS.mmmmmm


# def py_time():
#     """
#
#     """
#
#     t = time.time()
#     st = time.localtime(t)
#
#     # format:struct_time -> str
#     s = time.strftime(dtf, st)
#     logger.info(type(s))  # str
#
#     # parse:str -> struct_time
#     st = time.strptime(s, dtf)
#     logger.info(type(st))  # struct_time
#
#     # 时间戳
#     ts = time.mktime(st)
#     logger.info('timestamp:%f' % ts)
#     pass


def py_datetime():
    """

    """

    # today:datetime = datetime.today()
    # tz 可设置时区timezone
    now: datetime = datetime.now()

    # format:datetime -> str
    s: str = now.strftime(dtf)

    # parse:str -> datetime
    dt: datetime = datetime.strptime(s, dtf)

    """
    date:year,month,day
    time:hour,minute,second

    datetime    
    tzinfo
    
    """
    logger.info('year:%d' % dt.year)
    logger.info('month:%d' % dt.month)
    logger.info('day:%d' % dt.day)
    logger.info('hour:%d' % dt.hour)
    logger.info('minute:%d' % dt.minute)
    logger.info('second:%d' % dt.second)
    logger.info('month first:%s' % now.replace(day=1))

    d: date = dt.date()
    logger.info('weekday:%d' % d.weekday())  # 0:星期一

    t: time = dt.time()
    logger.info('microsecond:%d' % t.microsecond)

    pass


def py_timedelta():
    """时间差值"""

    """
    folds
    weeks
    days
    hours
    minutes
    seconds
    microseconds
    milliseconds
    
    """

    one_day = timedelta(days=1)
    yesterday = datetime.now() - one_day

    logger.info('yesterday:%s' % yesterday.strftime(dtf))

    data_format = '%Y-%m-%d'
    start_date = datetime.now()
    end_date = datetime.strptime('2023-08-18', data_format)
    while start_date < end_date:
        logger.info(start_date.strftime(data_format))
        start_date += one_day
        pass

    pass


def py_relativedelta():
    """
    相对Relative
    years
    months
    days
    leapdays
    hours
    minutes
    seconds
    microseconds
    绝对Absolute
    year
    month
    day
    hour
    minute
    second
    microsecond

    """
    # 相对差值
    one_month = relativedelta(months=1)
    last_month = datetime.now() - one_month
    logger.info(last_month)

    current = date.today()
    delta = relativedelta()
    delta.days = 1
    delta.months = 1
    target = current - delta
    logger.info(target)
    pass


if __name__ == '__main__':
    # py_time()

    # py_datetime()

    # py_timedelta()

    py_relativedelta()

    pass
